package com.wswy.wyjk.ui.uitls;

import android.text.TextUtils;

import com.blankj.utilcode.util.EncryptUtils;
import com.my.httpapi.api.baseUtils.LogUtils;
import com.wswy.wyjk.api.Constant;

import java.net.URLEncoder;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class ScUtil {

    public static String ucSign(TreeMap<String, String> upstream) {
        return ucSign(Constant.CLIENT_KEY, upstream);
    }

    public static String ucSignForOrc(TreeMap<String, String> upstream, String apiKey) {
        final StringBuilder builder = mapToHttpString(upstream, true);
        builder.append("&app_key=").append(apiKey);
        return bytes2HexString(EncryptUtils.encryptMD5(builder.toString().getBytes())).toUpperCase();

    }

    private static String ucSign(String appKey, Map<String, String> treeMap) {
        final StringBuilder builder = mapToHttpString(treeMap, true);
        builder.append("&key=").append(appKey);
        LogUtils.e(builder.toString());
        return bytes2HexString(EncryptUtils.encryptMD5((builder.toString()).getBytes()));
    }


    private static StringBuilder mapToHttpString(Map<String, String> treeMap, boolean encodeValue) {
        StringBuilder sb = new StringBuilder();
        try {

            Set<Map.Entry<String, String>> entrySet = treeMap.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                if (!TextUtils.isEmpty(entry.getValue())) {
                    sb.append(entry.getKey()).append("=");
                    if (encodeValue) {
                        //php 兼容
                        sb.append(URLEncoder.encode(entry.getValue(), "UTF-8").replaceAll("\\*", "%2A"));
                    } else {
                        sb.append(entry.getValue());
                    }
                    sb.append("&");
                }
            }
            if (!treeMap.isEmpty()) {
                sb.deleteCharAt(sb.length() - 1);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return sb;
    }

    private static final char hexDigits[] =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    private static String bytes2HexString(final byte[] bytes) {
        if (bytes == null) return null;
        int len = bytes.length;
        if (len <= 0) return null;
        char[] ret = new char[len << 1];
        for (int i = 0, j = 0; i < len; i++) {
            ret[j++] = hexDigits[bytes[i] >>> 4 & 0x0f];
            ret[j++] = hexDigits[bytes[i] & 0x0f];
        }
        return new String(ret);
    }

    public static byte[] xor(byte[] data, byte[] salt) {
        int len = data.length;
        int lenKey = salt.length;
        int i = 0;
        int j = 0;
        while (i < len) {
            if (j >= lenKey) {
                j = 0;
            }
            data[i] = (byte) (data[i] ^ salt[j]);
            i++;
            j++;
        }
        return data;
    }
}
